package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.CatgroyBean;
import com.scsj.supermarket.bean.HomeClassifyBean;
import com.scsj.supermarket.utils.SkipUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类
 */

public class CatgroyAdapter extends RecyclerView.Adapter<CatgroyAdapter.MyViewHolder>  {

    private LayoutInflater minflater;
    private Context mContext;
    protected List<HomeClassifyBean.DataBean.AppGoodsClassPt2VoListBean> mDatas = new ArrayList<>();
    private String title;

    public CatgroyAdapter(Context context) {
        this.mContext = context;
        minflater = LayoutInflater.from(context);
    }

    public void addAllData(List<HomeClassifyBean.DataBean.AppGoodsClassPt2VoListBean> mDatas,String title) {
        this.mDatas.addAll(mDatas);
        this.title = title;
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = minflater.inflate(R.layout.item_catgroy_content, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    public void add(int pos) {
//        mDatas.add(pos, "insert to");+
        notifyItemChanged(pos);
    }

    public void delte(int pos) {
        mDatas.remove(pos);
        notifyItemChanged(pos);
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, final int i) {
//        HomeClassifyBean.DataBean.AppGoodsClassPt2VoListBean itemBean = mDatas.get(i);

        myViewHolder.title_tv.setText(title);
        CatgroyItemAdapter itemAdapter = new CatgroyItemAdapter(mContext,mDatas);
        myViewHolder.recycler_view.setAdapter(itemAdapter);
        itemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                SkipUtils.toGoodsSort(mContext);

            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDatas != null) {
//            return mDatas.size();
            return 1;
        } else {
            return 0;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView title_tv;
        RecyclerView recycler_view;

        public MyViewHolder(View itemView) {
            super(itemView);
            title_tv = itemView.findViewById(R.id.title_tv);
            recycler_view = itemView.findViewById(R.id.recycler_view);
            recycler_view.setLayoutManager(new GridLayoutManager(mContext, 3) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
        }
    }
}
